package demoMod.stsconsole.powers;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.characters.AbstractPlayer;
import demoMod.stsconsole.core.AbstractCreature;
import demoMod.stsconsole.helpers.DamageInfo;
import demoMod.stsconsole.interfaces.ChangeVulnerableMultiplierSubscriber;
import demoMod.stsconsole.interfaces.DamageSubscriber;
import demoMod.stsconsole.interfaces.TurnStartSubscriber;

import java.lang.reflect.InvocationTargetException;

public class VulnerablePower extends AbstractPower implements DamageSubscriber, TurnStartSubscriber {
    public static final String ID = "VulnerablePower";
    private float multi;

    public VulnerablePower(AbstractCreature owner, int amount) {
        super(ID, owner, amount);
        this.type = PowerType.DEBUFF;
    }

    private void changeMultiplier() {
        this.multi = 1.5F;
        ConsoleEntry.triggerSubscribers(ChangeVulnerableMultiplierSubscriber.class,
                "onChangeVulnerableMultiplier",
                new Class[]{AbstractCreature.class},
                new Object[]{this.owner},
                (method, params, subscriber) -> {
                    try {
                        this.multi *= (float) method.invoke(subscriber, params);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
        );
    }

    @Override
    public String getUpdatedDescriptions() {
        changeMultiplier();
        return String.format(this.description, (int)(this.multi * 100 - 100), this.amount[0]);
    }

    @Override
    public float onAttackedToChangeDamage(float damageAmount, DamageInfo info, AbstractCreature target) {
        if (info.type == DamageInfo.DamageType.NORMAL && this.amount[0] > 0 && target == this.owner) {
            changeMultiplier();
            return damageAmount * multi;
        }
        return damageAmount;
    }

    @Override
    public void atTurnStart(boolean isPlayer) {
        if (this.owner instanceof AbstractPlayer == isPlayer) {
            this.amount[0]--;
        }
        if (this.amount[0] <= 0) {
            this.onRemove();
        }
    }
}
